<?php
/**
 * appRain v 0.1.x
 *
 * LICENSE
 *
 * This source file is subject to the MIT license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/mit-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@apprain.com so we can send you a copy immediately.
 *
 * @copyright  Copyright (c) 2010 appRain, Inc. (http://www.apprain.com)
 * @license    http://www.opensource.org/licenses/mit-license.php MIT license
 *
 * HELP
 * 
 * Official Website 
 * http://www.apprain.com/
 *
 * Download Link
 * http://www.apprain.com/download
 *
 * Documents Link
 * http ://www.apprain.com/documents
 */
class pageController extends appRain_Base_Core
{
    public $name = 'Page';

    /**
     * This function will reander the home page
     * 
     * @return null
     */
    private function index()
    {
    }

    /**
     * Mange static page
     * 
     * @parameter page_id integer
     * @return null
     */
    public function manageAction( $action = "update", $page_id = NULL )
    {	
        // Check access
        $errors = Array();
        $this->setAdminTab('page_manager');

        $this->addons = array('validation','rich_text_editor');

        // Save page
        if( !empty( $this->data ))
        {
        	if( $action == 'create')$content = App::Load("Model/Page")->find("name='{$this->data['Page']['name']}' AND fkey=" . $this->getUserStatusId());
        	else $content = App::Load("Model/Page")->findById($page_id);

        	if( isset($this->post['Button']['button_delete']))
        	{
        		App::Load("Model/Page")->DeleteById($page_id);
        		$this->redirect("/page/manage");
        		exit;
        	}
        	else
        	{
	        	$this->data['Page']['fkey']= $this->getUserStatusId();
				
	            $result = App::Load("Model/Page")->Save($this->data);	
                $errors = $result->getErrorInfo();

        	    if(empty($errors))
                {
        	   	    $this->redirect("/page/manage/update/" . $result->getId());
        	   	    exit;
                }
        	}
        }
        
        if( $action == 'update')
        {
	        // Get page information
	        $page_arr = $this->PageManager()
	        				 ->Pages("name_only");
			 
	        if( empty($page_arr))
            {
                $this->redirect("/page/manage/create");
                exit;
            }
	        $this->set( 'static_page_arr', $page_arr);
	
	        $page_id = isset(  $page_id )?  $page_id : key( $page_arr ) ;
	        $this->set('page_id',$page_id); 
	
	        $page_current = $this->PageManager()
	        				 	 ->Pages( $page_id );
	        				 
	        $this->set("page_current",$page_current );
        }

        $title = isset($page_current['title']) ? $page_current['title'] : 'Crate Page';        
        $this->set("action",$action );
        $this->page_title= "{$title}" . app::__def()->SysConfig('ADMIN_PAGE_TITLE_SAPARATOR') . "Page Manager";
        $this->set("errors",$errors);

    }
    
    /**
     * View page content by default it set to about us
     *
     * @parameter page_name string
     * @return null
     */
    public function viewAction( $page_name = 'about_us' ) 
    {
		$page_content = $this->staticPageNameToMetaInfo($page_name);
		
        $this->set( 'page_content' ,$page_content );
		
		$this->set("section_title",$page_content['title']);
		$this->set("selected",$page_name);
    }
}
